package com.mediacenter.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.mediacenter.R;
import com.mediacenter.activity.VideoActivity;
import com.mediacenter.base.OnlineVideo;
import com.mediacenter.utils.XmlReaderHelper;

public class CCTVFragment extends Fragment implements OnItemClickListener{
	private static final String TAG = "TVFragment";
	private DataAdapter mAdapter;
	private static ArrayList<OnlineVideo> root = new ArrayList<OnlineVideo>();
	private String TVID;
	private String defaultTVID = "01";

	public static CCTVFragment newInstance(String s) {
		CCTVFragment newFragment = new CCTVFragment();
		Bundle bundle = new Bundle();
		bundle.putString("TVID", s);
		newFragment.setArguments(bundle);
		return newFragment;

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 初始化Fragment
		Bundle args = getArguments();
		TVID = args != null ? args.getString("TVID") : defaultTVID;
		root = XmlReaderHelper.getVideos(getActivity(), TVID);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.online_tv_item, null, false);
		ListView onlinetv_listview = (ListView) view.findViewById(R.id.online_tv_listview);
		mAdapter = new DataAdapter(getActivity(), root);
		onlinetv_listview.setAdapter(mAdapter);
		onlinetv_listview.setOnItemClickListener(this);
		return view;

	}

	// 完成Fragment的初始化，不对前面不是已经初始化好了么？
	// 这个方法主要是初始化那些你需要你的父Activity或者Fragment的UI已经被完整初始化才能初始化的元素。
	// 具体你看图中的位置你能看出点端倪来。比如UI的元素没被解析反射进来，你还想用？ 想得美，等它初始化完，你再用。
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		// 这个Bundle会被传入到onCreate、onCreateView
		super.onSaveInstanceState(savedInstanceState);
	}

	@Override
	public void onDestroyView() {
		// 清除所有跟视图相关的资源
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "TestFragment-----onDestroy");
	}

	@Override
	public void onDetach() {
		super.onDetach();
	}

	/** 数据适配 */
	private class DataAdapter extends BaseAdapter {
		private Context ctx;
		private ArrayList<OnlineVideo> mdata;

		public DataAdapter(Context context, ArrayList<OnlineVideo> data) {
			ctx = context;
			mdata = data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mdata.size();
		}

		@Override
		public OnlineVideo getItem(int position) {
			// TODO Auto-generated method stub
			return mdata.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final OnlineVideo item = mdata.get(position);
			if (convertView == null) {
				final LayoutInflater mInflater = getActivity().getLayoutInflater();
				convertView = mInflater.inflate(R.layout.fragment_online_item, null);
			}
			ImageView thumbnail = (ImageView) convertView.findViewById(R.id.online_thumbnail);
			if (item.iconId > 0)
				thumbnail.setImageResource(item.iconId);
			else
				thumbnail.setImageDrawable(null);
			((TextView) convertView.findViewById(R.id.online_title)).setText(item.title);

			return convertView;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		OnlineVideo item = mAdapter.getItem(position);
		Intent intent = new Intent(getActivity(), VideoActivity.class);
		intent.setData(Uri.parse(item.url));
		intent.putExtra("displayName", item.title);
		startActivity(intent);
		
	}

}
